package com.ebelter.btcomlib.models.bluetooth.base;



import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;


import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;
import com.ebelter.btcomlib.models.bluetooth.bean.SendMessage;

import com.ebelter.btcomlib.models.bluetooth.interfaces.IConnectStationCallback;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.List;


public class BaseManager extends BaseHandle{
    private static final String TAG="BaseManager";


//    //添加一个蓝牙状态监听器
//    public void addBluetoothStationListener(IBlueStationListener listener){
//        BlueManager.getInstance().addBluetoothStationListener(listener);
//    }
//
//
//    public void removeBluetoothStationListener(IBlueStationListener listener){
//        BlueManager.getInstance().removeBluetoothStationListener(listener);
//    }
//
//    //移除所有的蓝牙状态监听器
//    public void clearAllBluetoothStationListener(){
//        BlueManager.getInstance().clearAllBluetoothStationListener();
//    }




    protected BluetoothAdapter mBluetoothAdapter;
    protected ServiceGatt mServiceGatt;



    public BaseManager(Context context, ProductStyle productStyle, String serviceUUID, String[] writeUUIDs,
                       String[] readUUIDs, boolean hasSendThread,long ServiceGatt_SendThread_interval){
        super(context);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mServiceGatt = new ServiceGatt(context, productStyle, serviceUUID,
                writeUUIDs,readUUIDs,hasSendThread,ServiceGatt_SendThread_interval);
    }

    /**
     * 判断bluetooth是否开启
     */
    public boolean isBluetoothOpen() {
        return BlueManager.getInstance().isBluetoothOpen();
    }

//    /**
//     * 打开蓝牙
//     */
//    public void openBluetooth() {
//        BlueManager.getInstance().openBluetooth();
//    }

    public void openBluetooth2(Activity activity,int requestCode) {
        BlueManager.getInstance().openBluetooth2(activity,requestCode);
    }
//    /**
//     * 关闭蓝牙
//     */
//    public void closeBluetooth() {
//        BlueManager.getInstance().closeBluetooth();
//    }

    //判断设备是否支持蓝牙
    public boolean isSupportBluetooth(){
        return BlueManager.getInstance().isSupportBluetooth();
    }


    public void setBlueConnectStationCallback(IConnectStationCallback mIConnectStationCallback) {
      //  IConnectStationCallback mIBlueConnectStationCallback = mIConnectStationCallback;
        if (mServiceGatt!=null){
            mServiceGatt.setConnectCallback(mIConnectStationCallback);
        }

    }

    public String getConnectBluetoothAddress(){
        if (mServiceGatt!=null)return mServiceGatt.getConnectBluetoothAddress();
        return null;
    }


    public BluetoothDevice getConnectBluetooth(){
        if (mServiceGatt!=null)return mServiceGatt.getConnectBluetooth();
        return null;
    }

    //判断蓝牙设备是否连接
    public boolean isConnect() {
        return mServiceGatt != null && mServiceGatt.isConnect();
    }

    //退出的清除操作
    public void exit(){
        if (mServiceGatt!=null){
            mServiceGatt.exit();
            mServiceGatt=null;
        }
    }


    public void disConnect() {
        disConnect(true);
    }
    public void disConnect(boolean sendfront) {
        if (mServiceGatt != null) {
            mServiceGatt.clearBlueGatt(sendfront);
        }
    }

    public String getConnectAddress() {
        if (mServiceGatt != null) {
            return mServiceGatt.getConnectBluetoothAddress();
        }
        return null;
    }



    public void connectDevice(String address) {
        LogUtils.i(TAG,"--BaseManager--connectDevice address = "+address);
        if (mBluetoothAdapter!=null&&mServiceGatt!=null){
          //  String connectBluetoothAddress = address;
            BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(address);
            mServiceGatt.connectBluetoothDevice(bluetoothDevice);
        }
    }



    public void connectDevice(BluetoothDevice bluetoothDevice) {
        if (bluetoothDevice!=null&&mServiceGatt != null){
            LogUtils.i(TAG,"--BaseManager--connectDevice address = "+bluetoothDevice.getAddress()+"---name = "+bluetoothDevice.getName());
            mServiceGatt.connectBluetoothDevice(bluetoothDevice);
        }
    }


    //下发信息到秤上的一个方法
    public boolean addSendMsg(SendMessage msg) {
        return mServiceGatt != null && mServiceGatt.addSendMsg(msg);
    }
    //下发信息到秤上的一个方法
    public boolean addSendMsgs(List<SendMessage> msgs) {
        return mServiceGatt != null && mServiceGatt.addSendMsgs(msgs);
    }

}
